Package de.yaams.extensions.rgssproject

Source Code of de.yaams.extensions.rgssproject.RTP

/**
*
*/
package de.yaams.extensions.rgssproject;

import java.io.File;
import java.util.HashMap;

import javax.swing.JButton;

import org.apache.commons.lang.SystemUtils;
import org.ini4j.Wini;

import de.yaams.maker.helper.FileHelper;
import de.yaams.maker.helper.Log;
import de.yaams.maker.helper.NetHelper;
import de.yaams.maker.helper.SwingHelper;
import de.yaams.maker.helper.SystemHelper;
import de.yaams.maker.helper.WindowsRegistry;
import de.yaams.maker.helper.extensions.ExtentionManagement;
import de.yaams.maker.helper.extensions.IExtension;
import de.yaams.maker.helper.gui.AE;
import de.yaams.maker.helper.gui.YActionDialog;
import de.yaams.maker.helper.gui.YDialog;
import de.yaams.maker.helper.gui.YEx;
import de.yaams.maker.helper.gui.form.FormButton;
import de.yaams.maker.helper.gui.form.core.FormBuilder;
import de.yaams.maker.helper.gui.form.core.FormHeader;
import de.yaams.maker.helper.gui.icons.IconCache;
import de.yaams.maker.helper.integration.EditorIntegration;
import de.yaams.maker.helper.language.I18N;
import de.yaams.maker.programm.YAamsCore;
import de.yaams.maker.programm.environment.YLevel;
import de.yaams.maker.programm.project.Project;

/**
* @author Praktikant
*
*/
public class RTP {

  public final static String TITLE = g("Titles"), WINDOWSKIN = g("Windowskins"), GAMEOVER = g("Gameovers"),
      TRANSITION = g("Transitions"), TILESET = g("Tilesets"), PANORAMA = g("Panoramas"), ICON = g("Icons"), FOG = g("Fogs"),
      CHARACTER = g("Characters"), BATTLERS = g("Battlers"), BATTLEBACK = g("Battlebacks"), AUTOTILE = g("Autotiles"),
      ANIMATION = g("Animations");

  public final static String BGM = a("BGM"), BGS = a("BGS"), ME = a("ME"), SE = a("SE");

  protected static HashMap<Integer, HashMap<String, File>> rtps;

  /**
   * Check installed rtp & options
   */
  public static void init() {
    // read all rtps
    rtps = new HashMap<Integer, HashMap<String, File>>();
    rtps.put(1, new HashMap<String, File>());
    rtps.put(2, new HashMap<String, File>());

    // check default rtps
    final String[] id = new String[] { "Standard", "RTP-Tunes", "Standard" };
    final String[] name = new String[] { "XP-Standard", "XP-Tunes", "VX-Standard" };
    final int rgss[] = new int[] { 1, 1, 2 };
    for (int i = 0, l = id.length; i < l; i++) {
      isRTPinstalled(id[i], rgss[i]);
    }

    // add rtp options
    ExtentionManagement.add("form.options.rgss", new IExtension() {

      @Override
      public void work(HashMap<String, Object> objects) {
        final FormBuilder f = (FormBuilder) objects.get("form");

        // add rtp
        f.addHeader("rtp", new FormHeader(I18N.t("RTP"), "rtp").setColumn(4));

        // add buttons
        for (int i = 0, l = id.length; i < l; i++) {
          boolean e = isRTPinstalled(id[i], rgss[i]) != null;
          final String key = "rtp." + rgss[i] + "_" + id[i];
          final int j = i;
          f.addElement(key, new FormButton(I18N.t(e ? "RTP {0} ist installiert" : "RTP {0} installieren", name[j]), e ? "rtp_ok"
              : "rtp_add", e ? null : new AE() {

            @Override
            public void run() {
              if (RTP.installRTP(id[j], rgss[j])) {
                JButton b = (JButton) f.getElement(key).getElement();
                b.setText(I18N.t("RTP {0} ist installiert", name[j]));
                b.setIcon(IconCache.get("rtp_ok"));
                b.setEnabled(false);
              }
            }
          }));
        }

      }
    });
  }

  /**
   * Helpermethod, who create all missing folders
   *
   * @param p
   */
  public static void checkCreateGraphicAudioFolder(Project p) {
    for (String s : new String[] { TITLE, WINDOWSKIN, GAMEOVER, TRANSITION, TILESET, PANORAMA, ICON, FOG, CHARACTER, BATTLERS,
        BATTLEBACK, AUTOTILE, ANIMATION, BGM, BGS, ME, SE }) {
      // exist folder?
      File f = new File(p.getPath(), s);
      if (!f.exists() || !f.canRead()) {
        FileHelper.mkdirs(f);
      }
    }
  }

  /**
   * Helpermethod
   *
   * @return
   */
  private static String g(String folder) {
    return "Graphics" + File.separator + folder;
  }

  /**
   * Helpermethod
   *
   * @return
   */
  private static String a(String folder) {
    return "Audio" + File.separator + folder;
  }

  /**
   * Helpermethod to check the rtps from the project and check, if their
   * exist, and will ask the user to install
   *
   * @param p
   */
  public static void readRTP(final Project p) {
    // has rtps?
    if (p.getCache().containsKey("rtpFiles")) {
      return;
    }

    HashMap<String, File> rtpFiles = new HashMap<String, File>();
    // read game ini
    try {
      Wini ini = new Wini(RGSSProjectHelper.getGameIniFile(p));

      // rgss1?
      if (RGSSProjectHelper.getRGSSVersion(p) == 1) {
        // read rtp
        for (String s : new String[] { "RTP1", "RTP2", "RTP3" }) {
          // exist?
          String rtp = ini.get("Game", s);
          if (rtp != null && rtp.length() > 0) {
            if (installRTP(rtp, 1)) {
              p.getCache().put(s, rtp);
              rtpFiles.put(rtp, rtps.get(1).get(rtp));
            }
          } else {
            p.getCache().put(s, "");
          }
        }
      }

      // rgss2?
      if (RGSSProjectHelper.getRGSSVersion(p) == 2) {
        // read rtp
        // exist?
        String rtp = ini.get("Game", "RTP");
        if (rtp != null && rtp.length() > 0) {
          if (installRTP(rtp, 2)) {
            p.getCache().put("rtp", rtp);
            rtpFiles.put(rtp, rtps.get(2).get(rtp));
          }
        } else {
          p.getCache().put("rtp", "");
        }
      }

    } catch (Throwable t) {
      YEx.info("Can not read " + RGSSProjectHelper.getGameIniFile(p) + " for rtp", t);
    }
    p.getCache().put("rtpFiles", rtpFiles);
  }

  /**
   * Helpermethod to get the filename in the yaams version
   *
   * @param name
   * @param rgssVersion
   * @return
   */
  protected static File getRTPFilename(String name, int rgssVersion) {
    return new File(YAamsCore.programPath, "rtp" + File.separator + rgssVersion + File.separator + name);
  }

  /**
   * Helpermethod to install an rtp, if not exist
   *
   * @param name
   * @param rgssVersion
   * @return true, exist, false otherwise
   */
  public static boolean installRTP(String name, int rgssVersion) {
    // exist?
    if (rtps.get(rgssVersion).containsKey(name)) {
      return true;
    }

    // can install
    File f = isRTPinstalledGui(name, rgssVersion, true);
    if (f != null) {
      rtps.get(rgssVersion).put(name, f);
      return true;
    }

    return false;
  }

  /**
   * Helpermethod to check if exist, or ask the user to download/install it
   *
   * @param name
   * @param rgssVersion
   * @return null, or File for the location
   */
  protected static File isRTPinstalledGui(String name, int rgssVersion, boolean first) {
    // has it?
    File f = isRTPinstalled(name, rgssVersion);
    if (f != null) {

      // contains graphics.exe?
      if (new File(f, "Graphics.exe").exists()) {
        // fix for standard?
        if (name.equals("Standard")
            && !new File(f, "Graphics" + File.separator + "Titles" + File.separator + "001-Title01.jpg").exists()) {
          if (YDialog
              .askUser(
                  I18N.t("RTP {0} enthält unzulässige Dateien", name),
                  "rtp.install.graphics",
                  "rtp",
                  I18N.t("Graphics.exe wird nicht unterstützt. Neuinstallation des RTPs wird empfohlen. Laden des Projektes trotzdem fortführen?"),
                  I18N.t("Projekt öffnen"), I18N.t("RTP neu installieren"), "disk", "setup")) {
            return f;
          }

        }
      } else {
        return f;
      }
    }

    int erg = 0;

    if (!first || YLevel.IS_ADVANCED) {
      // ask for install
      YActionDialog y = new YActionDialog("rtp.install", I18N.t("RTP {0} wurde nicht gefunden.", name));
      y.addLink("setup_rtp", I18N.t("Installieren (empfohlen)"),
          I18N.t("Überprüft online, ob die Version existiert und installiert Sie."));
      y.addLink("folder_search", I18N.t("Ordner auswählen"), I18N.t("Ermöglicht selbst manuell den RTP Ordner auswählen"));
      y.addLink("cancel", I18N.CANCEL, I18N.t("Bindet kein RTP ein. Kann zu Nebeneffekten führen, wenn Ressourcen verwendet werden."));

      erg = y.show();
    }

    // get it
    if (erg == 0) {

      YDialog.ok(
          I18N.t("RTP {0} wird nun runtergeladen & installiert", name),
          I18N.t("{0} prüft online, ob das RTP verfügbar ist. Danach wird es runtergeladen und installiert. Abhängig von der Internetgeschwindigkeit kann der Prozess einige Zeit in anspruch nehmen."),
          "setup_wait");

      // ask online
      String url = NetHelper.getContentAsString("http://www.yaams.de/file/?typ=rtp&windows=" + SystemUtils.IS_OS_WINDOWS + "&name="
          + name + "&rgss=" + rgssVersion);

      // found?
      if (url == null || url.length() == 0 || url.equals("null")) {
        return isRTPinstalledGui(name, rgssVersion, false);
      }

      // get file ending
      String[] s = url.split("/");

      // download
      File ex = new File(YAamsCore.tmpFolder, s[s.length - 1]);
      NetHelper.downloadFile(ex, url);

      // extract or run?
      if (url.endsWith(".zip")) {
        // create folder
        FileHelper.mkdirs(getRTPFilename(name, rgssVersion));
        // extract
        FileHelper.extractArchive(ex, getRTPFilename(name, rgssVersion));
      } else {
        // start
        SystemHelper.viewFile(ex);

        final String rtpName = name;
        // inform user
        new SwingHelper(true) {

          @Override
          public void run() {
            YDialog.ok(I18N.t("RTP {0} wird nun installiert.", rtpName),
                I18N.t("Die Installation wurde nun gestartet. Drücken Sie ok erst, wenn die Installation beendet ist."),
                "setup_wait");

          }
        };
      }

      return isRTPinstalledGui(name, rgssVersion, false);
    }

    // ask user
    if (erg == 1) {
      File rtpFolder = EditorIntegration.openDialog(false, true)[0];
      if (rtpFolder == null) {
        return isRTPinstalledGui(name, rgssVersion, false);
      } else {
        return rtpFolder;
      }
    }

    // get it
    return null;

  }

  /**
   * Helpermethod to check if exist
   *
   * @param name
   * @param rgssVersion
   * @return null, or File for the location
   */
  protected static File isRTPinstalled(String name, int rgssVersion) {

    // exist in yaams?
    if (getRTPFilename(name, rgssVersion).exists()) {
      return getRTPFilename(name, rgssVersion);
    }

    // windows system?
    // supported?
    if (!SystemUtils.IS_OS_WINDOWS) {
      return null;

    }

    try {

      // has rtp?
      // 64b System fix?
      String path = WindowsRegistry.getKeySz(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\"
          + (SystemUtils.OS_ARCH.contains("64") ? "Wow6432Node\\" : "") + "Enterbrain\\RGSS\\RTP", name);

      // add it
      return new File(path);
    } catch (Throwable t) {
      Log.ger.info("Can't find rtp " + name + " for rgss" + rgssVersion);
      t.printStackTrace();
    }
    return null;
  }

  /**
   * Get all rtps of this project
   *
   * @param p
   * @return
   */
  @SuppressWarnings("unchecked")
  public static HashMap<String, File> getRTPs(Project p) {
    // run?
    if (!p.getCache().containsKey("rtpFiles")) {
      readRTP(p);
    }

    // get it
    return (HashMap<String, File>) p.getCache().get("rtpFiles");
  }

  /**
   * @return the rtps
   */
  public static HashMap<Integer, HashMap<String, File>> getRtps() {
    return rtps;
  }
}
TOP

Related Classes of de.yaams.extensions.rgssproject.RTP

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.